package gfo

/*Fighter
* -------------------------------------------
* Fighter represents the playable characters in Super Smash Bros.*/
class Fighter {
    String name
    String description//--this is causing errors, gonna leave it out for the moment
    String imgFile
    static hasMany = [matches: Matchship]

    static constraints = {
        matches(nullable: true)
        description(nullable: true,
                    maxSize:  500)
        imgFile(nullable: true)
    }

    String toString(){
        "$name"
    }

    /*mostPlayedBy
    * ----------------------------------------
    * Examines all the matches this Fighter has been used for and returns the Player that has
    * played as it most. Used in Fighter:show.gsp*/
    Player mostPlayedBy(){
        def players = [:]
        for (player in Player.list()){
            players[player] = 0
        }
        for (match in Match.list()){
            for (ms in match.players){
                if(ms.character.id==this.id){
                    players[ms.player] ++
                }
            }
        }
        def keys = players.keySet().toList()
        def vals = players.values().toList()
        if (vals.max()==0){
            return null
        }
        return keys[vals.indexOf(vals.max())]
    }

    /*mostWonBy
    * ----------------------------------------
    * Examines all the matches this Fighter has been used for and returns the Player that has
    * won the most times as it. Used in Fighter:show.gsp*/
    Player mostWonBy(){
        def players = [:]
        for (player in Player.list()){
            players[player] = 0
        }
        for (match in Match.list()){
            for (ms in match.players){
                if (ms.winner && ms.character.id == this.id){
                    players[ms.player] ++
                }
            }
        }
        def keys = players.keySet().toList()
        def vals = players.values().toList()
        if (vals.max()==0){
            return null
        }
        return keys[vals.indexOf(vals.max())]
    }
}
